<!DOCTYPE html>
<html>
<head>
    <script src="../../../resources/js-test.js"></script>
</head/>
<body>
<script>
    description("Make sure the same deserialization of the state object is used every time (both in the history object and popstate events).");

    window.jsTestIsAsync = true;
    if (window.testRunner) {
        testRunner.clearBackForwardList();
        testRunner.waitUntilDone();
    }

    shouldBeDefined("history.state");

    // Create a new object.
    var stateObject = ["a"];

    // Use it as the state object in a replaceState.  This clones the object.
    history.replaceState(stateObject, null, null);

    shouldBeTrue("history.state === history.state");

    // Since the actual history.state is a structured clone, it should not match our original object.
    shouldBeTrue("history.state !== stateObject");

    // Now let's refetch a copy of history.state to store;
    stateObject = history.state;

    // Our reference and the history.state itself should be the same.  This is now Adam's original assertion.
    shouldBeTrue("history.state === stateObject");

    // Now let's do a pushstate to add a history entry.
    history.pushState(null, null, null);

    // Now add a handler for the popstate event.
    var popStateEvent;
    window.onpopstate = function(e) {
        debug("\nInside popstate event\n");
        popStateEvent = e;
        // Our stored reference to stateObject will not match the current state object, as it is a structured clone of the history item's state object.
        shouldBeTrue("history.state !== stateObject");
        // Our stored reference to stateObject will not match the state object in this pop state event, as it is the same as history.state which is a structured clone of the history item's state object.
        shouldBeTrue("popStateEvent.state !== stateObject");
        // The event's state object and the current state object should match.
        shouldBeTrue("popStateEvent.state === history.state");

        setTimeout(finishJSTest, 0);
    }

    // Now let's go back to our original history entry which has a state object that we've stored a reference to already.
    // This will fire our popstate event handler above.
    window.onload = function() {
        history.back();
    };
</script>
</body>
</html>
